var menu = [];
var totalPage = 0;
var totalCnt = 0;
var parentNameData = new Array();

$(document).ready(function() {
	showLoading();
	$('title', parent.document).html($('title').text());
	setBaseUrl();
	createTableHead("menuThead", menuTable, search);
	$ajaxList('menuName', getMenuName);
	$(".leftClick").each(function(){
		 $(this).click(function(){
			 var id = $(this).attr('id');
			 $(this).find('img#userSelectRight').toggle();
		     $(this).find('img#userSelectBottom').toggle();
		 	if (id == 'typeList') {
	    		$('#menuLevel').hide();
	    		$('#levelList').find('img#userSelectRight').show();
	    		$('#levelList').find('img#userSelectBottom').hide();
	    		$('#menuType').toggle();
	    	} else if (id == 'levelList') {
	    		$('#menuType').hide();
	    		$('#typeList').find('img#userSelectRight').show();
	    		$('#typeList').find('img#userSelectBottom').hide();
	    		$('#menuLevel').toggle();
	    		
	    	}
		 });
	});
	search();
	setTimeout(hideLoading, 500);
});

function getMenuName(data) {
	var length = data.length;
	for (var i = 0; i < length; i++) {
		parentNameData[i] = data[i].resourcesName;
	}
}
function search(){
	$ajax('searchMenu', 'menuSearch', searchMenu);
}
function nameValue() {
	var resourcesName = $("#name").val();
	$("#ResourcesName").val(resourcesName);
	search();
}
function roleIDVal(value, clickID, ID, flag) {
	formReset("menuSearch");
	$("#" + ID).val(value);
	$(".clickBgBlue").removeClass("clickBgBlue");
	if (clickID != '') {
		$("#" + clickID).addClass("clickBgBlue");
	}
	if (flag != "") {
		 $(".leftClick").next().children().hide();
		 $('.userSelectRight').show();
		 $('.userSelectBottom').hide();
	}
	search();
}
function searchMenu(data) {
	totalPage = data && data.totalPage || 0;
	totalCnt = data && data.totalCnt || 0;
	menu = data && data.menuList || [];
	createTableBody("menuTbody", menuTable, menu);
	$(function(){
		$("#name").autocomplete({
			source: parentNameData,
			minLength: 0
		});
	});
}
function resetSearch(){
	setTimeout(search, 500);
}
function chkBefore(index, chkFlag){
	var checkbox =document.getElementsByName("chk"); 
	for(var i=0; i<checkbox.length; i++){
		checkbox[i].checked = false; 
	}
	checkbox[index].checked = true; 
	if(chkFlag == "update"){
		update(index);
	}else if(chkFlag == "delete"){
		del();
	}
}
//添加
function save() {
	deleteFlag = "save";
	addTitle("menuSave");
	allTable();
}

//修改
function update() {
	var checkbox = getCheckbox("chk");
	var count = checkbox && checkbox.count;
	var index = checkbox && checkbox.row_index;
    if (count == 1) {
    	$('#title').html('');
    	deleteFlag = "update";
    	addTitle("menuUpdate");
    	allTable(index);
    } else if (count == 0) {
    	showMessge(messageContent["Select.NotFound"]);
    } else {
    	showMessge(messageContent["Select.OnlyOne"]);
    }
}

function allTable(index){
	createDetailHead("detailThead", menuTable, 1);
	createDetailBody("detailTbody", menuTable,menu[index], 1);
	if(deleteFlag == "update") {
		$("#layer").val(parseInt(menu[index].layer));
		if(menu[index].type == "PAGE"){
			$("#type").val("PAGE");
		}else if(menu[index].type == "BUTTON"){
			$("#type").val("BUTTON");
		}else if(menu[index].type == "LINK"){
			$("#type").val("LINK");
		}else if(menu[index].type == "MENU"){
			$("#type").val("MENU");
		}else if(menu[index].type == "LIST"){
			$("#type").val("LIST");
		}else if(menu[index].type == "POP"){
			$("#type").val("POP");
		}else{
			$("#type").val("OTHER");
		}
		if(menu[index].enabled == true){
			$("#enabled").val(1);
		}else{
			$("#enabled").val(0);
		}
		$("#resourcesID").val(menu[index].resourcesID);
		$("#url").attr("readonly", false);
		$("#resourcesID").attr("readonly", false);
		$('#confirm').show();
	}
	if(deleteFlag == "save") {
		$("#resourcesID").val(0);
	}
	$(function(){
		$("#parentName").autocomplete({
			source: parentNameData,
			minLength: 0
		});
	});
	$("#save").modal("show");
}

function confirm(){
	return checkForm("saveForm", menuTable);
}
function getUrl(){
	if(deleteFlag == "save"){
		return 'saveMenu';
	} else if(deleteFlag == "update"){
		return 'updateMenu';
	} else if(deleteFlag == "delete"){
		return 'deleteMenu';
	}
}
function saveMenu(data){
	if(deleteFlag == "save"){
	     var result = data && data.result;
	     if (result == "Success") {
	    	 showMessge(messageContent["Insert.Success"]);
	    	 $('#save').modal('hide');
	    	 formReset("saveForm");
	    	 search();
	    	 $ajaxList('menuName', getMenuName);
	     } else if (result == "NotFound"){
	    	 showMessge(messageContent["Insert.Failed"]);
	     } else {
	    	 showMessge(messageContent["Insert.Error"]);
	     }
	     search();
	}else if(deleteFlag == "update"){
	     var result = data && data.result;
	     if (result == "Success"){
	    	 showMessge(messageContent["Update.Success"]);
	    	 $('#save').modal('hide');	    	 
	    	 formReset("saveForm");
	    	 search();
	    	 $ajaxList('menuName', getMenuName);
	     } else if (result == "NotFound"){
	    	 showMessge(messageContent["Update.Failed"]);
	     } else{
	    	 showMessge(messageContent["Update.Error"]);
	     }
	     search();
	}
}

function getData(){
	return {resourcesID: getCheckDataList(menu, "resourcesID")};
}

function sure(data){
    var result = data && data.result;
    if (result == "Success") {
		showMessge(messageContent["Delete.Success"]);
		$ajaxList('menuName', getMenuName);
	} else if (result == "NotFound") {
		showMessge(messageContent["Delete.NotFound"]);
	} else {
		showMessge(messageContent["Delete.Error"]);
	}
	search();
}